package com.gensdai.leliang.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gensdai.leliang.R;
import com.gensdai.leliang.adapter.ProductDetailDescListAdapter;
import com.gensdai.leliang.base.BaseActivity;
import com.gensdai.leliang.common.bean.OffLineStoreBean;
import com.gensdai.leliang.entity.parseBean.BaseParse;
import com.gensdai.leliang.remoteInterface.RetrofitFactory;
import com.gensdai.leliang.retrofitUtils.BaseObserver;
import com.gensdai.leliang.retrofitUtils.ModelFilteredFactory;
import com.gensdai.leliang.retrofitUtils.exception.APIException;
import com.gensdai.leliang.view.NoScrollListView;
import com.gensdai.leliang.view.Toaster;
import com.gensdai.leliang.view.banner.PageInfo;
import com.gensdai.leliang.view.banner.SimpleImageBanner;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import retrofit2.HttpException;

public class OfflineStoreInfoActivity extends BaseActivity {

    public static final String EXTRA_SID = "extrasid";

    @BindView(R.id.ProductGalley)
    SimpleImageBanner ProductGalley;
    @BindView(R.id.shopname)
    TextView shopname;
    @BindView(R.id.shophone)
    TextView shophone;
    @BindView(R.id.shopaddress)
    TextView shopaddress;
    @BindView(R.id.productDescImg)
    NoScrollListView productDescImg;

    private String sid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        sid = getIntent().getStringExtra(EXTRA_SID);
        getInfo();
    }

    private void getInfo() {

        if (sid != null) {
            Map<String, String> params = new HashMap<>();
            params.put("shopId", sid);
            ModelFilteredFactory
                    .compose(RetrofitFactory.getInstance().API().getOfflineStoreInfo(params)) //跟生命周期绑定一起)
                    .compose(this.<BaseParse<OffLineStoreBean>>bindUntilEvent(ActivityEvent.DESTROY))
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            //启动监听前的处理，一般用于显示dialog什么的
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<OffLineStoreBean>(this) {

                        @Override
                        public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
                            //监听  用d判断是否注册  会优先执行
                            super.onSubscribe(d);
                            Log.d("OnDisposable", "OnDisposable");
                        }

                        @Override
                        public void onHandleError(int code, String message) {
                            //正确请求到数据 但是success为false
                            Toaster.showOneToast(code + ":" + message);

                        }

                        @Override
                        public void onFailure(final Throwable e) {
                            super.onFailure(e);
                            if (e instanceof APIException) {
                                APIException exception = (APIException) e;
                                Toast.makeText(getApplicationContext(), exception.message, Toast.LENGTH_SHORT).show();
                            } else if (e instanceof UnknownHostException) {
                                Toast.makeText(getApplicationContext(), "请打开网络", Toast.LENGTH_SHORT).show();
                            } else if (e instanceof SocketTimeoutException) {
                                Toast.makeText(getApplicationContext(), "请求超时", Toast.LENGTH_SHORT).show();
                            } else if (e instanceof ConnectException) {
                                Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
                            } else if (e instanceof HttpException) {
                                Toast.makeText(getApplicationContext(), "请求异常" + ((HttpException) e).code(), Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getApplicationContext(), "请求失败", Toast.LENGTH_SHORT).show();
                            }

                        }


                        @Override
                        public void onRequestEnd() {
                            super.onRequestEnd();

                        }

                        @Override
                        public void onHandleSuccess(OffLineStoreBean offLineStoreBean) {
                            initView(offLineStoreBean);
                        }
                    });
        }
    }

    private void initView(final OffLineStoreBean bean) {

        if (bean.shopLogo != null) {
            List<PageInfo> infos = new ArrayList<>();
            for (String s : bean.shopLogo) {
                PageInfo pif = new PageInfo();
                pif.setImgPath(s);
                infos.add(pif);
            }
            if(infos.size() > 0){
                ProductGalley.setSource(infos).startScroll();
            }
        }

        if (!TextUtils.isEmpty(bean.shopName)) {
            shopname.setText(bean.shopName);
        }
        if (!TextUtils.isEmpty(bean.shopPhone)) {
            shophone.setText(bean.shopPhone);
            //拨打电话
            shophone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent1 = new Intent(Intent.ACTION_DIAL);
                    intent1.setData(Uri.parse("tel:" + bean.shopPhone));
                    startActivity(intent1);
                }
            });
        }
        if (!TextUtils.isEmpty(bean.shopAddress)) {
            shopaddress.setText(bean.shopAddress);
        }

        if(bean.detailPic != null){
            ProductDetailDescListAdapter adapter = new ProductDetailDescListAdapter(this);
            productDescImg.setAdapter(adapter);
            adapter.initAdd(bean.detailPic);
        }


        ProductGalley.setFocusable(true);
        ProductGalley.setFocusableInTouchMode(true);
        ProductGalley.requestFocus();
    }

    @Override
    protected String getTitleStr() {
        return "体验店详情";
    }

    @Override
    protected boolean isShowBackButton() {
        return true;
    }

    @Override
    protected boolean isCustomTitle() {
        return false;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_offline_store_info;
    }
}
